//Documentation complete
/**
 * @author Jeff
 * @version 2009AP05
 */

package control;

import broker.*;
import problemDomain.*;
import problemDomain.EmployeeAccountBean.*;
import static problemDomain.ErrorBean.ErrorCode.*;

import java.util.List;

/**
 * Controls object for employee account operations.
 */
public class EmployeeAccountControl extends Control {
	
	/* Attributes */
	
	private EmployeeAccountBroker eab;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs an <code>EmployeeAccountControl</code>.
	 * @throws ErrorBean if the database is not available
	 */
	public EmployeeAccountControl () throws ErrorBean {
		eab = EmployeeAccountBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Creates a new employee account.
	 * @param position the account position (used for permissions)
	 * @param firstname the first name of the employee
	 * @param lastname the last name of the employee
	 * @param username the username of the employee account
	 * @param password the password of the employee account
	 * @param passcode the passcode of the employee account
	 * @throws ErrorBean if the input data are not correct
	 */
	public void create (String username, String password, 
			String firstname, String lastname, 
			String position, String passcode) throws ErrorBean 
			{
		EmployeeAccountBean bean = new EmployeeAccountBean(
			0, username, password, firstname, lastname, checkPosition(position), checkPasscode(passcode));
		eab.persist(bean);
	}
	
	/**
	 * Searches for employee accounts which match the search criteria.
	 * @param firstname the first name to search for (<code>""</code> excludes this field when searching)
	 * @param lastname the last name to search for (<code>""</code> excludes this field when searching)
	 * @param username the username to search for (<code>""</code> excludes this field when searching)
	 * @param position the account poition to search for (<code>""</code> excludes this field when searching)
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<EmployeeAccountBean> search 
			(String firstname, String lastname, String username, String position) throws ErrorBean {
		EmployeeAccountBean bean = new EmployeeAccountBean();
		if (firstname != null && !"".equals(firstname))
			bean.setFirstname(firstname);
		
		if (lastname != null && !"".equals(lastname))
			bean.setLastname(lastname);
		
		if (username != null && !"".equals(username))
			bean.setUsername(username);
		
		if (position != null && !"".equals(position))
			bean.setPosition(checkPosition(position));
		
		return eab.search(bean);
	}
	
	/**
	 * Updates an employee account.
	 * @param id the ID of the employee account
	 * @param position the account position (used for permissions)
	 * @param firstname the first name of the employee
	 * @param lastname the last name of the employee
	 * @param username the username of the employee account
	 * @param password the password of the employee account
	 * @param passcode the passcode of the employee account
	 * @throws ErrorBean if the input data are not correct
	 */
	public void update (String id, String position, 
			String firstname, String lastname, String username,
			String password, String passcode) throws ErrorBean {
		EmployeeAccountBean bean = new EmployeeAccountBean(checkID(id),username,password,firstname,lastname,checkPosition(position),checkPasscode(passcode));
		eab.persist(bean);
	}
	
	/**
	 * Deletes an employee account.
	 * @param id the ID number of the employee account
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String id) throws ErrorBean {
		eab.remove(new EmployeeAccountBean(checkID(id)));
	}
	
	/**
	 * Lists all employees.
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if there is an error retrieving the data 
	 */
	public List<EmployeeAccountBean> listAll () throws ErrorBean {
		List<EmployeeAccountBean> list = eab.listAll();
		return list;
	}
	
	/**
	 * Validates a username and password. 
	 * @param username the username of the employee account
	 * @param password the password of the employee account
	 * @return the authenticated employee
	 * @throws ErrorBean if the input data are not correct
	 */
	public EmployeeAccountBean validate (String username, String password) throws ErrorBean {
		EmployeeAccountBean eb = new EmployeeAccountBean();
		eb.setUsername(checkUsername(username));
		eb.setPassword(checkPassword(password));
		return eab.validate(eb);
	}
	
	/**
	 * Validates a passcode.
	 * @param passcode the passcode of the employee account
	 * @return the authenticated employee
	 * @throws ErrorBean if the input data are not correct
	 */
	public EmployeeAccountBean validate (String passcode) throws ErrorBean {
		EmployeeAccountBean eb = new EmployeeAccountBean();
		eb.setPasscode(checkPasscode(passcode));
		return eab.validate(eb);
	}
	
	/* END Methods */
	
	
	/* Data Checks */
	
	private int checkID (String id) throws ErrorBean {
		return checkInt(id, EMPLOYEE_ID_INVALID, this);}
	
	private Position checkPosition (String position) throws ErrorBean {
		return checkPosition(position, EMPLOYEE_PRIVILEGES_NOT_SPECIFIED, this, false);}
	
	private long checkPasscode (String passcode) throws ErrorBean {
		return checkLong(passcode, EMPLOYEE_PASS_CODE_INVALID, this, false);}
	
	private String checkUsername (String username) throws ErrorBean {
		return checkString(username, EMPLOYEE_INCORRECT_USERNAME_OR_PASSWORD, this, false);}
	
	private String checkPassword (String password) throws ErrorBean {
		return checkString(password, EMPLOYEE_INCORRECT_USERNAME_OR_PASSWORD, this, false);}
	
	/* Data Checks */
	
}
